package com.yb.framework.neo4j.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yb.framework.neo4j.repository.Neo4jRepository;
import com.yb.framework.neo4j.service.Neo4jService;

@Service
@Transactional
public class Neo4jServiceImpl implements Neo4jService {
	
	@Autowired
	private Neo4jRepository neo4jRepository;

	@Override
	public void createNode(String label, String nodeUuid, String nodeName) {
		// TODO Auto-generated method stub
		neo4jRepository.createNode(label, nodeUuid, nodeName);
	}
	
	@Override
	public void createTemplateNode(String label, String nodeUuid, String nodeName, 
			String parentNodeUuid, String... shapeNodeUuids) {
		// TODO Auto-generated method stub
		neo4jRepository.createTemplateNode(label, nodeUuid, nodeName, parentNodeUuid, shapeNodeUuids);
	}

	@Override
	public void createThingNode(String label, String nodeUuid, String nodeName, 
			String tempalteNodeUuid, String... shapeNodeUuids) {
		// TODO Auto-generated method stub
		neo4jRepository.createThingNode(label, nodeUuid, nodeName, tempalteNodeUuid, shapeNodeUuids);
	}

	@Override
	public void createParamNode(String label, String nodeUuid, String nodeName, 
			String relationLabel, String relationUuid) {
		// TODO Auto-generated method stub
		neo4jRepository.createParamNode(label, nodeUuid, nodeName, relationLabel, relationUuid);
	}

	@Override
	public void updateNode(String label, String nodeUuid, String nodeName) {
		// TODO Auto-generated method stub
		neo4jRepository.updateNode(label, nodeUuid, nodeName);
	}

	@Override
	public void updateNode(String label, String nodeUuid, String nodeName, 
			String tempalteNodeUuid, String... shapeNodeUuids) {
		// TODO Auto-generated method stub
		neo4jRepository.updateNode(label, nodeUuid, nodeName, tempalteNodeUuid, shapeNodeUuids);
	}

	@Override
	public void deleteNode(String label, String nodeUuid) {
		// TODO Auto-generated method stub
		neo4jRepository.deleteNode(label, nodeUuid);
	}

	@Override
	public void deleteLabel(String label) {
		// TODO Auto-generated method stub
		neo4jRepository.deleteLabel(label);
	}

	@Override
	public JSONObject getNode(String label, String nodeUuid) {
		// TODO Auto-generated method stub
		return neo4jRepository.getNode(label, nodeUuid);
	}

	@Override
	public JSONObject getAllNodes(String label, String nodeUuid) {
		// TODO Auto-generated method stub
		JSONObject result = neo4jRepository.getNode(label, nodeUuid);
		JSONObject childNodes = neo4jRepository.getChildNodes(label, nodeUuid);
		JSONObject allNodes = neo4jRepository.getAllChildNodes(childNodes, result);
		return allNodes;
	}

	@Override
	public JSONObject getChildNodes(String label, String nodeUuid) {
		// TODO Auto-generated method stub
		return neo4jRepository.getChildNodes(label, nodeUuid);
	}

	@Override
	public JSONObject getAllChildNodes(String label, String nodeUuid) {
		// TODO Auto-generated method stub
		JSONObject childNodes = neo4jRepository.getChildNodes(label, nodeUuid);
		JSONObject allChildNodes = neo4jRepository.getAllChildNodes(childNodes, new JSONObject());
		return allChildNodes;
	}
	
	public JSONArray getParamNames(String label, String nodeUuid) {
		JSONArray paramNames = new JSONArray();
		JSONObject childNodes = getChildNodes(label, nodeUuid);
		if(childNodes != null && childNodes.getJSONArray("nodeList") != null &&
				childNodes.getJSONArray("nodeList").size() > 0) {
			JSONArray nodeList = childNodes.getJSONArray("nodeList");
			for (int i = 0; i < nodeList.size(); i++) {
				JSONObject node = nodeList.getJSONObject(i);
				if("param".equals(node.getString("label"))) {
					paramNames.add(node.getString("name"));
				}
			}
		}
		return paramNames;
	}
	
	public JSONArray getAllParamNames(String label, String nodeUuid) {
		JSONArray allParamNames = new JSONArray();
		JSONObject allNodes = getAllNodes(label, nodeUuid);
		if(allNodes != null && allNodes.getJSONArray("nodeList") != null &&
				allNodes.getJSONArray("nodeList").size() > 0) {
			JSONArray nodeList = allNodes.getJSONArray("nodeList");
			for (int i = 0; i < nodeList.size(); i++) {
				JSONObject node = nodeList.getJSONObject(i);
				if("param".equals(node.getString("label"))) {
					allParamNames.add(node.getString("name"));
				}
			}
		}
		return allParamNames;
	}
}
